package com.qf.service;

import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(propagation = Propagation.SUPPORTS,  readOnly = true,rollbackFor = Exception.class)
public class AccountServiceImpl implements AccountService{

    @Autowired
    AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void transfer(String fromName, String toName, double money) {
        Account fromAccount = accountDao.selectByName(fromName);
        Account toAccount = accountDao.selectByName(toName);

        //更新余额
        fromAccount.setMoney(fromAccount.getMoney() - money);
        toAccount.setMoney(toAccount.getMoney() + money);

        //更新数据库
        accountDao.update(fromAccount);
        //System.out.println(1/0);
        accountDao.update(toAccount);
    }
}
